package rippleutils;

import java.util.Random;

public class RandomPassword {

    public static void main(String[] args) {
        int repititions = 10;
        if(args.length > 0) {
          repititions = Integer.parseInt(args[0]);
        }
        
        Random r = new Random();

        // printMostAscii(); // That is where the alphabet came from.
        
        for(int i=0; i<repititions; i++) {
          randomPassword1(r);
        }

        for(int i=0; i<repititions; i++) {
          wordLikeRandomPassword(r);
        }
    }

    private static void wordLikeRandomPassword(Random r) {
        StringBuffer semiRandomPassword = new StringBuffer();
        appendGobbledeGookWord(semiRandomPassword, r);
        appendRandomDigits(semiRandomPassword, r, 3);
        appendGobbledeGookWord(semiRandomPassword, r);
        System.out.println(semiRandomPassword);
    }

    private static void randomPassword1(Random r) {
        StringBuffer totallyRandomPassword = new StringBuffer();
        appendTotallyRandomPassword(totallyRandomPassword, r);
        System.out.println(totallyRandomPassword);
    }

    private static void appendRandomDigits(StringBuffer semiRandomPassword, Random r, int numberOfDigits) {
        for (int i = 0; i < numberOfDigits; i++) {
            semiRandomPassword.append(r.nextInt(10));
        }
    }

    private static StringBuffer appendGobbledeGookWord(StringBuffer s, Random r) {
        appendWordStart(s, r);
        appendWordMiddle(s, r);
        appendWordEnding(s, r);

        return s;
    }

    private static void appendWordStart(StringBuffer s, Random r) {
        if (heads(r)) {
            s.append(getConsonant(r));
        } else {
            s.append(getConsonantDipthong(r));
        }
    }

    private static void appendWordMiddle(StringBuffer s, Random r) {
        if (heads(r)) {
            s.append(getVowel(r));
        } else {
            s.append(getVowelDipthong(r));
        }
    }

    private static void appendWordEnding(StringBuffer s, Random r) {
        double indicator = r.nextDouble();
        if (indicator < 1.0 / 3.0) {
            s.append(getConsonant(r));
        } else if (indicator < 2.0 / 3.0) {
            s.append(getConsonantDipthong(r));
        } else {
            s.append(getConsonantDipthongForSyllableEndings(r));
        }
    }

    private static String getConsonantDipthong(Random r) {
        String[] consonantDipthongs = { "th", "st", "sh", "ch" };
        String nextCharacter = getRandomStringFromStringArray(r, consonantDipthongs);
        return nextCharacter;
    }

    private static String getConsonantDipthongForSyllableEndings(Random r) {
        String[] consonantDipthongsForSyllableEndings = { "ght", "ce", "se", "te", "bb", "ff", "gg", "ll", "nn", "pp", "rr", "ss", "tt", "zz", "ble", "ng", "nd", "mb", "nt", "ke" };
        String nextCharacter = getRandomStringFromStringArray(r, consonantDipthongsForSyllableEndings);
        return nextCharacter;
    }

    private static String getVowelDipthong(Random r) {
        String[] vowelDipthongs = { "ee", "ie", "ea", "ie", "or", "oa", "oo", "ou" };
        String nextCharacter = getRandomStringFromStringArray(r, vowelDipthongs);
        return nextCharacter;

    }

    private static String getRandomStringFromStringArray(Random r, String[] stringArray) {
        int randomInteger = r.nextInt(stringArray.length);
        String randomString = stringArray[randomInteger];
        return randomString;
    }

    private static char getConsonant(Random r) {
        String consonants = "bcdfghjklmnpqrstvwxz";
        char nextCharacter = getRandomCharFromString(r, consonants);
        return nextCharacter;
    }

    private static char getVowel(Random r) {
        String vowels = "aeiouy";
        char nextCharacter = getRandomCharFromString(r, vowels);
        return nextCharacter;
    }

    private static char getRandomCharFromString(Random r, String sourceString) {
        int randomInteger = r.nextInt(sourceString.length());
        char randomCharacter = sourceString.charAt(randomInteger);
        return randomCharacter;
    }

    private static boolean heads(Random r) {
        return r.nextDouble() > 0.5;
    }

    private static void appendTotallyRandomPassword(StringBuffer totallyRandomPassword, Random r) {
        // String alphabet =
        // "!#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
        String alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        // Use if only a reduced range of symbols allowed.

        int passwordLength = 10;
        for (int i = 0; i < passwordLength; i++) {
            char nextCharacter = getRandomCharFromString(r, alphabet);
            totallyRandomPassword.append(nextCharacter);
        }
    }

    public static void printMostAscii() {
        for (char i = 33; i < 127; i++) {
            System.out.print(i);
        }
        System.out.println();
    }

}
